package com.tzbank.wisdom.service.impl;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.tzbank.esb.util.EsbConsumerUtil;
import com.tzbank.utils.CodeUtil;
import com.tzbank.utils.ResultCode;
import com.tzbank.utils.ResultUtil;
import com.tzbank.wisdom.constant.SysConstans;
import com.tzbank.wisdom.service.SysSettingService;
import com.tzbank.wisdom.utils.RequestMap;
import com.tzbank.wisdom.utils.TransCodeEnum;

import lombok.extern.slf4j.Slf4j;

/**
 * zhangzehuan
 */
@Service(value = "sysSettingService")
@Slf4j
public class SysSettingServiceImpl implements SysSettingService {
	
	
    @Override
    public Map<String, Object> saveNotice(Map<String, Object> reqBody) {
    	log.info("设置预留信息请求参数:{}", JSON.toJSONString(reqBody));
        
        if (StringUtils.isBlank(reqBody.get("notice")+"")) {
        	return ResultUtil.resultMap("请输入预留信息", ResultCode.RESUTL_CODE_FAILURE.code(),null);
		}
        
        Map<String, Object> inputMap = new HashMap<String, Object>();
        Map<String, Object> reqSysHead = new HashMap<String, Object>();
        
        String clientSeqNo = reqBody.get("clientSeqNo").toString();
        String notice = reqBody.get("notice").toString();
        
        reqSysHead = RequestMap.getReqSysHead(TransCodeEnum.SYS_NOTICE.getServiceCode(), TransCodeEnum.SYS_NOTICE.getServiceSence(), "0110");
        inputMap = RequestMap.getInputMap(reqBody);
    	Map<String, Object> userMap = RequestMap.getInputUserInfo(clientSeqNo);
		if (userMap.get("clientNo")==null) {
			return userMap;
		}
		
		inputMap.putAll(userMap);
        
        inputMap.put("sysHead", reqSysHead);
        inputMap.put("reservedMessage", notice);
        
        Map<String, Object> result = EsbConsumerUtil.clientRequest(inputMap);
        if (CodeUtil.CODE_200.getCode().equals(result.get("code"))) {
        	return ResultUtil.resultMap("设置成功", ResultCode.RESULT_CODE_SUCCESS.code(), null);
        }
        return ResultUtil.resultMap(result.get("msg").toString(), ResultCode.RESUTL_CODE_FAILURE.code(), result.get("data"));

    }

    @Override
    public Map<String, Object> getProfession(Map<String,String> reqBody) {
       Map<String,Object> reqSysHead = RequestMap.getReqSysHead(TransCodeEnum.PROFESSION_LISTQRY.getServiceCode(), TransCodeEnum.PROFESSION_LISTQRY.getServiceSence(), "0110");
       Map<String, Object> inputMap = new HashMap<String, Object>();
       inputMap.put("sysHead", reqSysHead);
       String clientSeqNo = reqBody.get("clientSeqNo");
       Map<String, Object> userMap = RequestMap.getInputUserInfo(clientSeqNo);
       inputMap.putAll(userMap);
       if (userMap.get("clientNo")==null) {
			return userMap;
	   }
       Map<String, Object> result = EsbConsumerUtil.clientRequest(inputMap);
       if (CodeUtil.CODE_200.getCode().equals(result.get("code"))) {
       		return ResultUtil.resultMap("获取职业信息列表成功", ResultCode.RESULT_CODE_SUCCESS.code(), result.get("data"));
       }
       return ResultUtil.resultMap(result.get("msg").toString(), ResultCode.RESUTL_CODE_FAILURE.code(), result.get("data"));

    }

    @Override
    public Map<String, Object> updatePersonalInfo(Map<String, String> reqBody) {
        Map<String,Object> reqSysHead = RequestMap.getReqSysHead(TransCodeEnum.PERSONAL_INFO_UPDATE.getServiceCode(), TransCodeEnum.PERSONAL_INFO_UPDATE.getServiceSence(), "0110");
        Map<String, Object> inputMap = new HashMap<String, Object>();
        inputMap.put("sysHead", reqSysHead);
        String clientSeqNo = reqBody.get("clientSeqNo");
        Map<String, Object> userMap = RequestMap.getInputUserInfo(clientSeqNo);
        String occupationId = reqBody.get("occupationId");//职业信息
        if(StringUtils.isNotEmpty(occupationId)) {
        	inputMap.put("occupationId", occupationId);
        }
        String mobileNo = reqBody.get("mobileNo");//手机号
        if(StringUtils.isNotEmpty(mobileNo)) {
        	inputMap.put("mobileNo", mobileNo.toString());
        }
        inputMap.putAll(userMap);
        if (userMap.get("clientNo")==null) {
 			return userMap;
 	   }
        Map<String, Object> result = EsbConsumerUtil.clientRequest(inputMap);
        if (CodeUtil.CODE_200.getCode().equals(result.get("code"))) {
        		return ResultUtil.resultMap("个人信息修改成功", ResultCode.RESULT_CODE_SUCCESS.code(), result.get("data"));
        }
        return ResultUtil.resultMap(result.get("msg").toString(), ResultCode.RESUTL_CODE_FAILURE.code(), result.get("data"));

    }

    @Override
    public Map<String, Object> saveLoginName(Map<String, Object> map) {
        // 组装入参Map
        Map<String, Object> inputMap = new HashMap<String, Object>();
//        Map<String, Object> reqSysHead = new HashMap<String, Object>();
        Map<String, Object> reqSysHead = RequestMap.getReqSysHead(TransCodeEnum.LOGIN_ALIAS_EDIT.getServiceCode(),TransCodeEnum.LOGIN_ALIAS_EDIT.getServiceSence() , SysConstans.ABP_CODE, 
        		null, "304010");
        //调用核心计价因子报文头入参
//        reqSysHead.put(SysConstans.SYS_TRAN_CODE, TransCodeEnum.LOGIN_ALIAS_EDIT.getServiceCode());        // 服务码
//        reqSysHead.put(SysConstans.SYS_TRAN_SENCE, TransCodeEnum.LOGIN_ALIAS_EDIT.getServiceSence());                // 服务场景
//        reqSysHead.put("eClientNo", "500000040");
//        reqSysHead.put("userNo", "500000008");                //500000673
//        reqSysHead.put("sourceType", "PMBS");            //PMBS
//        reqSysHead.put("consumerSeqNo", UUID.randomUUID().toString());        //123456
//        reqSysHead.put("tranTimestamp", new Date().getTime() + "");        //new Date().getTime()+
        //调用核心计价因子入参
        inputMap.put("sysHead", reqSysHead);
        inputMap.put("userId", map.get("userId"));
        inputMap.put("oldUserId", map.get("oldUserId"));
        Map<String, Object> result = EsbConsumerUtil.clientRequest(inputMap);
        if (!CodeUtil.CODE_200.getCode().equals(result.get("code"))) {
            return ResultUtil.resultMap(result.get("msg").toString(), ResultCode.RESUTL_CODE_FAILURE.code(),
                    result.get("data"));
        }
        return ResultUtil.resultMap("成功", ResultCode.RESULT_CODE_SUCCESS.code(), result.get("data"));
    }

	@Override
	public Map<String, Object> cardValidate(Map<String, String> reqBody) {
        Map<String, Object> inputMap = new HashMap<String, Object>();
//        Map<String, Object> reqSysHead = new HashMap<String, Object>();
        Map<String, Object> reqSysHead = RequestMap.getReqSysHead(TransCodeEnum.CARD_NO_VALIDATE.getServiceCode(),TransCodeEnum.CARD_NO_VALIDATE.getServiceSence() , SysConstans.ABP_CODE, 
        		null, "304010");
//    	reqSysHead.put(SysConstans.SYS_TRAN_CODE,TransCodeEnum.CARD_NO_VALIDATE.getServiceCode()); 		// 服务码
//		reqSysHead.put(SysConstans.SYS_TRAN_SENCE, TransCodeEnum.CARD_NO_VALIDATE.getServiceSence()); 	
//		reqSysHead.put("targetSysId", "304010");//调用系统代码  核心是301010  信贷是304010
//		reqSysHead.put("consumerId", SysConstans.ABP_CODE);//智脑系统代码
		inputMap.put("sysHead", reqSysHead);
		reqSysHead.put("globalId", reqBody.get("globalId"));//证件号
		
        Map<String, Object> result = EsbConsumerUtil.clientRequest(inputMap);
        if (!CodeUtil.CODE_200.getCode().equals(result.get("code"))) {
            return ResultUtil.resultMap(result.get("msg").toString(), ResultCode.RESUTL_CODE_FAILURE.code(),
                    result.get("data"));
        }
        return ResultUtil.resultMap("成功", ResultCode.RESULT_CODE_SUCCESS.code(), result.get("data"));
	}

	@Override
	public Map<String, Object> getPersonalInfo(Map<String, String> reqBody) {
		
		// 组装入参Map
		Map<String, Object> inputMap = new HashMap<String, Object>();
		
		Map<String, Object> reqSysHead= RequestMap.getReqSysHead(TransCodeEnum.PERSONAL_INFO.getServiceCode(),TransCodeEnum.PERSONAL_INFO.getServiceSence(), 
				"", SysConstans.ABP_CODE, "108060");
		
		Map<String, Object> userMap = RequestMap.getInputUserInfo((String)reqBody.get("clientSeqNo"));
		Map<String, Object> dealResult=null;
		if (userMap.get("clientNo")==null) {
			return userMap;
		}
		reqSysHead.put("branchId", reqBody.get("branchNo")); //发送方机构
		reqSysHead.putAll(userMap);
		inputMap.put("sysHead", reqSysHead);
		inputMap.put("clientNo", userMap.get("clientNo")); 
		Map<String, Object> result = EsbConsumerUtil.clientRequest(inputMap); // 
		
		if (!CodeUtil.CODE_200.getCode().equals(result.get("code"))) {
//			Map<String,Object> resultMap = getProfession(reqBody);
//			
//			if(CodeUtil.CODE_200.getCode().equals(result.get("code"))) {
//				JSONArray array = JSONArray.fromObject(resultMap.get("queryResultArray"));
//				for(int i=0;i<array.size();i++) {
//					Object jsonObj = array.get(i);
//					
//				}
//			}
			return ResultUtil.resultMap(result.get("msg").toString(), ResultCode.RESUTL_CODE_FAILURE.code(),
					result.get("data"));
		}else {
			dealResult=	(Map<String, Object>) result.get("data");
			dealResult.remove("head");
		}
		return ResultUtil.resultMap( ResultCode.RESULT_CODE_SUCCESS.desc(), ResultCode.RESULT_CODE_SUCCESS.code(), dealResult);
	
	}
}
